<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="职别" prop="staffLevels">
            <el-select v-model="queryParams.staffLevels" clearable placeholder="请选择">
              <el-option v-for="item in categoryOptions" :key="item.jobCategoryId" :label="item.jobName"
                         :value="item.jobCategoryId"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="注册时间">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                            type="daterange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="staffList" border>
      <el-table-column align="center" label="注册时间" prop="registerTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="员工姓名" prop="staffName"/>
      <el-table-column align="center" label="性别" prop="staffSex">
        <template slot-scope="scope">
          {{ formatGender(scope.row.staffSex) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="年龄">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="身份证号" prop="staffIdcard"/>
      <el-table-column align="center" label="联系方式" prop="staffPhone"/>
      <el-table-column align="center" label="职别" prop="staffLevelsName"/>
      <el-table-column align="center" label="推荐人" prop="recommenStaffId"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:staff:edit']" size="mini" type="text"
                     @click="handleOnboarding(scope.row)">入职
          </el-button>
          <el-button v-hasPermi="['cld:staff:blacklist']" size="mini" type="text"
                     @click="handleInvalid(scope.row)">设为黑名单
          </el-button>
          <el-button v-hasPermi="['cld:staff:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="open" append-to-body title="个人信息录入"
               width="50%">
      <add-staff v-if="open" ref="addStaff" :staffId="staffId" @success="handleSubmitSuccess"></add-staff>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStaff">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>
  </div>
</template>

<script>
import {delStaff, listStaff,updateStaff } from "@/api/cld/staff";
import addStaff from "./addStaff.vue";
import {getListCategory} from '@/api/cld/category'

export default {
  name: "Staff",
  dicts: ['cld_staff_levels'],
  inject: ['formatGender'],
  components: {
    addStaff
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 职别列表
      categoryOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员花名册表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        staffStatus: 0
      },
      // 当前选中人员id
      staffId: null,
      dateRange: [],
    };
  },
  created() {
    this.getList();
    // 获取职别列表
    this.handleGetListCategory();
  },
  methods: {
    /** 查询人员花名册列表 */
    getList() {
      this.loading = true;
      listStaff(this.addDateRange(this.queryParams, this.dateRange, 'RegisterTime')).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 职别列表
    handleGetListCategory() {
      getListCategory().then(res => {
        this.categoryOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 无效
    handleInvalid(row) {
      this.$prompt('', '拉黑原因', {
        center: true,
        inputType: 'textarea',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        row.staffStatus = 4;
        row.blackReason = value;
        updateStaff(row).then(response => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.staffId = row.staffId;
      this.open = true;
    },
    // 提交
    submitStaff() {
      this.$refs.addStaff.submitForm();
    },
    // 提交回调
    handleSubmitSuccess() {
      this.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除员工姓名为"' + row.staffName + '"的数据项？').then(function () {
        return delStaff(row.staffId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 入职
    handleOnboarding(row){
      this.$modal.confirm('是否将["' + row.staffName + '"]变更为已入职？').then(function () {
        row.staffStatus = 1;
        return updateStaff(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    }
  }
};
</script>
<style scoped>
::v-deep .el-dialog__body {
  margin: 5px 10px;
  border: 1px solid #ddcaca;
}

.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
